package java_thinking.unit_21.Exercise.Lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2019-08-11 9:53
 */

public class TestLockSupport {
    public void testPark() {
        LockSupport.park();
    }

    public void testParkBlocker() {
        LockSupport.park(this);
    }

    public static void main(String[] args) {

        TestLockSupport testLockSupport = new TestLockSupport();
        /**
         * 未将this作为park参数,日志输出结果
         * "maindemo" #1 prio=5 os_prio=0 tid=0x0000000002e13800 nid=0x4578 waiting on condition [0x0000000002c9f000]
         *    java.lang.Thread.State: WAITING (parking)
         * 	at sun.misc.Unsafe.park(Native Method)
         * 	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304)
         * 	at java_thinking.unit_21.Exercise.Lock.TestPark.testPark(TestPark.java:13)
         * 	at java_thinking.unit_21.Exercise.Lock.TestPark.maindemo(TestPark.java:27)
         */
//        testLockSupportPark.testPark();
        /**
         * 将this作为park参数，jvm日志输出为
         * "maindemo" #1 prio=5 os_prio=0 tid=0x0000000003303800 nid=0x700 waiting on condition [0x00000000031cf000]
         *    java.lang.Thread.State: WAITING (parking)
         * 	at sun.misc.Unsafe.park(Native Method)
         * 	- parking to wait for  <0x000000076b750ed0> (a java_thinking.unit_21.Exercise.Lock.TestLockSupportPark) //使用带 blocker 参数的 park 方法，线程堆枝可 以提供更多有关阻塞对象的信息。
         * 	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
         * 	at java_thinking.unit_21.Exercise.Lock.TestLockSupportPark.testParkBlocker(TestLockSupportPark.java:17)
         * 	at java_thinking.unit_21.Exercise.Lock.TestLockSupportPark.maindemo(TestLockSupportPark.java:43)
         *
         */
        testLockSupport.testParkBlocker();


    }
}

class TestParkNanos {
    public static void main(String[] args) {
        /**.
         * "maindemo" #1 prio=5 os_prio=0 tid=0x0000000002983800 nid=0x13f0 waiting on condition [0x00000000023af000]
         *    java.lang.Thread.State: TIMED_WAITING (parking)
         * 	at sun.misc.Unsafe.park(Native Method)
         * 	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:338)
         * 	at java_thinking.unit_21.Exercise.Lock.TestParkNanos.maindemo(TestLockSupport.java:53)
         */
        LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));

        /**
         * "maindemo" #1 prio=5 os_prio=0 tid=0x0000000003123800 nid=0x3ea4 waiting on condition [0x0000000002f9f000]
         *    java.lang.Thread.State: TIMED_WAITING (parking)
         * 	at sun.misc.Unsafe.park(Native Method)
         * 	- parking to wait for  <0x000000076b750a38> (a java_thinking.unit_21.Exercise.Lock.TestParkNanos)
         * 	at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
         * 	at java_thinking.unit_21.Exercise.Lock.TestParkNanos.maindemo(TestLockSupport.java:60)
         */
        LockSupport.parkNanos(new TestParkNanos(), TimeUnit.SECONDS.toNanos(5));
    }
}
